package com.ambulance.project.biz.controller.manager;

import com.ambulance.common.utils.SecurityUtils;
import com.ambulance.common.utils.poi.ExcelUtil;
import com.ambulance.framework.aspectj.lang.annotation.Log;
import com.ambulance.framework.aspectj.lang.enums.BusinessType;
import com.ambulance.framework.web.controller.BaseController;
import com.ambulance.framework.web.domain.AjaxResult;
import com.ambulance.framework.web.page.TableDataInfo;
import com.ambulance.project.manager.domain.BizDoctor;
import com.ambulance.project.manager.domain.BizNurse;
import com.ambulance.project.manager.mapper.BizDoctorMapper;
import com.ambulance.project.manager.mapper.BizNurseMapper;
import com.ambulance.project.manager.service.IBizNurseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * 护士管理Controller
 *
 * @author zr
 * @date 2020-05-11
 */
@RestController
@RequestMapping("/manager/nurse")
public class BizNurseController extends BaseController {
    @Autowired
    private IBizNurseService bizNurseService;

    @Autowired
    private BizNurseMapper bizNurseMapper;

    /**
     * 查询护士管理列表
     */
    //@PreAuthorize("@ss.hasPermi('manager:nurse:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizNurse bizNurse) {
        startPage();
        List<BizNurse> list = bizNurseService.selectBizNurseList(bizNurse);
        return getDataTable(list);
    }

    /**
     * 导出护士管理列表
     */
    //@PreAuthorize("@ss.hasPermi('manager:nurse:export')")
    @Log(title = "护士管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizNurse bizNurse) {
        List<BizNurse> list = bizNurseService.selectBizNurseList(bizNurse);
        ExcelUtil<BizNurse> util = new ExcelUtil<BizNurse>(BizNurse.class);
        return util.exportExcel(list, "nurse");
    }

    /**
     * 获取护士管理详细信息
     */
    //@PreAuthorize("@ss.hasPermi('manager:nurse:query')")
    @GetMapping(value = "/{nurseID}")
    public AjaxResult getInfo(@PathVariable("nurseID") Long nurseID) {
        return AjaxResult.success(bizNurseService.selectBizNurseById(nurseID));
    }

    /**
     * 新增护士管理
     */
    //@PreAuthorize("@ss.hasPermi('manager:nurse:add')")
    @Log(title = "护士管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizNurse bizNurse) {
        return toAjax(bizNurseService.insertBizNurse(bizNurse));
    }



    @PostMapping("/modifyPass")
    @ResponseBody
    public AjaxResult modifyPass(@RequestBody Map<String, Object> params)
    {
        String password = params.get("password").toString();
        String confirm_password = params.get("confirm_password").toString();
        Integer nurseID = Integer.parseInt(params.get("nurseID").toString());

        String pattern = "^[a-z0-9]{6,18}$";
        boolean isMatch = Pattern.matches(pattern, password);
        if (!isMatch) {
            return AjaxResult.error("密码格式不正确");
        }
        if (!password.equals(confirm_password)) {
            return AjaxResult.error("两次密码不正确");
        }

        BizNurse bizNurse = new BizNurse();
        bizNurse.setNurseID(nurseID.longValue());
        bizNurse.setPassword(SecurityUtils.encryptPassword(password));
        int i = bizNurseMapper.updateBizNurse(bizNurse);
        if (i > 0) {
            return AjaxResult.success("ok");
        }
        return AjaxResult.error("系统错误");
    }


    /**
     * 修改护士管理
     */
    //@PreAuthorize("@ss.hasPermi('manager:nurse:edit')")
    @Log(title = "护士管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizNurse bizNurse) {
        return toAjax(bizNurseService.updateBizNurse(bizNurse));
    }

    /**
     * 删除护士管理
     */
    //@PreAuthorize("@ss.hasPermi('manager:nurse:remove')")
    @Log(title = "护士管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{nurseIDs}")
    public AjaxResult remove(@PathVariable Long[] nurseIDs) {
        return toAjax(bizNurseService.deleteBizNurseByIds(nurseIDs));
    }

    /**
     * 冻结护士
     */
//    //@PreAuthorize("@ss.hasPermi('manager:doctor:remove')")
//    @Log(title = "医生管理", businessType = BusinessType.DELETE)
    @PutMapping("light/{nurseID}")
    public AjaxResult light(@PathVariable Long nurseID) {
        return toAjax(bizNurseService.lightBizNurse(nurseID));
    }
}
